package com.filetransfer.view.Components;
import com.filetransfer.utils.FontUtils;
import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import java.awt.*;

import static com.filetransfer.utils.StringFormat.formatSize;
public class StatisticsDataPanel extends JPanel {
    private final JLabel fileCountLabel;
    private final JLabel totalSizeLabel;
    private final JLabel downloadCountLabel;
    private final JLabel totalDownloadSizeLabel;
    public StatisticsDataPanel() {
        setLayout(new MigLayout("wrap 1", "[grow]", "[]20[]20[]20[]"));
        // 初始化标签
        fileCountLabel = createInfoLabelContent("获取中...");
        totalSizeLabel = createInfoLabelContent("获取中...");
        downloadCountLabel = createInfoLabelContent("获取中...");
        totalDownloadSizeLabel = createInfoLabelContent("获取中...");
        // 添加标签
        add(createInfoLabelTitle("文件数量"));
        add(fileCountLabel);
        add(createInfoLabelTitle("所有文件总大小"));
        add(totalSizeLabel);
        add(createInfoLabelTitle("总下载次数"));
        add(downloadCountLabel);
        add(createInfoLabelTitle("总下载总大小"));
        add(totalDownloadSizeLabel);
    }
    private JLabel createInfoLabelTitle(String title) {
        JLabel labelTitle = new JLabel(title);
        labelTitle.setFont(FontUtils.getFont(18));
        labelTitle.setHorizontalAlignment(SwingConstants.LEFT);
        return labelTitle;
    }
    private JLabel createInfoLabelContent(String content) {
        JLabel labelValue = new JLabel(content);
        labelValue.setFont(FontUtils.getFont(Font.BOLD, 24));
        labelValue.setHorizontalAlignment(SwingConstants.LEFT);
        return labelValue;
    }
    public void updateStatistics(String fileCount, String totalSize, String downloadCount, String totalDownloadSize) {
        fileCountLabel.setText(fileCount);
        try {
            long sizeInBytes = Long.parseLong(totalSize);
            totalSizeLabel.setText(formatSize(sizeInBytes));
        } catch (NumberFormatException e) {
            totalSizeLabel.setText("无效数据");
        }
        downloadCountLabel.setText(downloadCount);
        try {
            long downloadSizeInBytes = Long.parseLong(totalDownloadSize);
            totalDownloadSizeLabel.setText(formatSize(downloadSizeInBytes));
        } catch (NumberFormatException e) {
            totalDownloadSizeLabel.setText("无效数据");
        }
    }
}
